package com.ruoyi.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.core.domain.vo.SmsRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.core.domain.Orders;
import com.ruoyi.core.mapper.SmsRecordMapper;
import com.ruoyi.core.domain.SmsRecord;
import com.ruoyi.core.service.ISmsRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 快递发货短信通知记录Service业务层处理
 * 
 * @author lgc
 * @date 2020-12-01
 */
@Service
public class SmsRecordServiceImpl implements ISmsRecordService 
{

    @Autowired
    private SmsRecordMapper smsRecordMapper;

    /**
     * 查询快递发货短信通知记录
     * 
     * @param smsRecordId 快递发货短信通知记录ID
     * @return 快递发货短信通知记录
     */
    @Override
    public SmsRecord selectSmsRecordById(Long smsRecordId)
    {
        return smsRecordMapper.selectSmsRecordById(smsRecordId);
    }

    /**
     * 查询快递发货短信通知记录列表
     * 
     * @param smsRecord 快递发货短信通知记录
     * @return 快递发货短信通知记录
     */
    @Override
    public List<SmsRecordVo> selectSmsRecordList(SmsRecord smsRecord)
    {
        return smsRecordMapper.selectSmsRecordList(smsRecord);
    }

    /**
     * 新增快递发货短信通知记录
     * 
     * @param smsRecord 快递发货短信通知记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSmsRecord(SmsRecord smsRecord)
    {
        int rows = smsRecordMapper.insertSmsRecord(smsRecord);
        insertOrders(smsRecord);
        return rows;
    }

    /**
     * 修改快递发货短信通知记录
     * 
     * @param smsRecord 快递发货短信通知记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSmsRecord(SmsRecord smsRecord)
    {
        smsRecordMapper.deleteOrdersByOrdersId(smsRecord.getSmsRecordId());
        insertOrders(smsRecord);
        return smsRecordMapper.updateSmsRecord(smsRecord);
    }

    /**
     * 删除快递发货短信通知记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSmsRecordByIds(String ids)
    {
        smsRecordMapper.deleteOrdersByOrdersIds(Convert.toStrArray(ids));
        return smsRecordMapper.deleteSmsRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除快递发货短信通知记录信息
     * 
     * @param smsRecordId 快递发货短信通知记录ID
     * @return 结果
     */
    @Override
    public int deleteSmsRecordById(Long smsRecordId)
    {
        smsRecordMapper.deleteOrdersByOrdersId(smsRecordId);
        return smsRecordMapper.deleteSmsRecordById(smsRecordId);
    }

    /**
     * 新增快递发货信息
     * 
     * @param smsRecord 快递发货短信通知记录对象
     */
    public void insertOrders(SmsRecord smsRecord)
    {
        List<Orders> ordersList = smsRecord.getOrdersList();
        Long smsRecordId = smsRecord.getSmsRecordId();
        if (StringUtils.isNotNull(ordersList))
        {
            List<Orders> list = new ArrayList<Orders>();
            for (Orders orders : ordersList)
            {
//                orders.set(smsRecordId);
                list.add(orders);
            }
            if (list.size() > 0)
            {
                smsRecordMapper.batchOrders(list);
            }
        }
    }
}
